package class06_堆和堆排序.test;

import java.util.Arrays;
import java.util.PriorityQueue;

public class Code04_SortArrayDistanceLessK {
    public static void sortedArrDistanceLessK(int[] arr, int k){
        if(arr == null || arr.length < 2){
            return;
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        int index = 0;
        for (; index < Math.min(arr.length - 1, k - 1) ; index ++) {
                heap.add(arr[index]);
        }
        int i = 0;
        for (; index < arr.length  ; index ++) {
            heap.add(arr[index]);
            arr[i++] = heap.poll();
        }
        while (!heap.isEmpty()){
            arr[i++] = heap.poll();
        }
    }

    public static void main(String[] args) {
        int testTimes = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTimes; i++) {
            int K = (int)(Math.random() * maxSize + 1);
            int[] arr = randomArrayNoMoveMoreK(maxSize, maxValue, K);
            int[] arr2 = copyArray(arr);
            int[] arr3 = copyArray(arr);
            sortedArrDistanceLessK(arr2, K);
            Arrays.sort(arr3);
            if(!isEqual(arr2, arr3)){
                succeed = false;
                printArray(arr);
                printArray(arr2);
                printArray(arr3);
            }
        }
        System.out.println(succeed?"Good!!!":"Fuck!!!!");
    }

    private static void printArray(int[] arr) {
        if(arr == null){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    private static boolean isEqual(int[] arr2, int[] arr3) {
        if(arr2 == null && arr3 != null){
            return false;
        }
        if(arr2 != null && arr3 == null){
            return false;
        }
        if(arr2 == null && arr3 == null){
            return true;
        }
        if(arr2.length != arr3.length){
            return false;
        }
        for (int i = 0; i < arr2.length; i++) {
            if(arr2[i] != arr3[i]){
                return false;
            }
        }
        return true;
    }

    private static int[] copyArray(int[] arr) {
        if(arr == null) {
            return new int[0];
        }
        int[] help = new int[arr.length];
        for (int i = 0; i < help.length; i++) {
            help[i] = arr[i];
        }
        return help;
    }

    private static int[] randomArrayNoMoveMoreK(int maxSize, int maxValue, int k) {
        int size = (int)(Math.random() * maxSize + 1);
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = (int)(Math.random() * maxValue + 1);
        }
        Arrays.sort(arr);
        boolean[] isSwap = new boolean[arr.length];
        for (int i = 0; i < arr.length; i++) {
            int j = Math.min(i + (int)(Math.random() * k + 1), arr.length - 1);
            if(!isSwap[i] && !isSwap[j]){
                isSwap[i] = true;
                isSwap[j] = true;
                int temp = arr[i];
                arr[j] = temp;
                arr[i] = temp;
            }
        }
        return arr;
    }
}
